package com.atguigu.mapreduce.demo.wordcount;

import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Reducer;

import java.io.IOException;

/**
 * @version V1.0
 * @author: guangcheng
 * @date: 2021/11/13 20:06
 * @description:
 */
public class WordCountReduce extends Reducer<Text, IntWritable, Text, IntWritable> {
    private IntWritable outK = new IntWritable();


    /**
     *     public void run(Reducer<KEYIN, VALUEIN, KEYOUT, VALUEOUT>.Context context) throws IOException, InterruptedException {
     *         this.setup(context);
     *
     *         try {
     *             while(context.nextKey()) {
     *                 this.reduce(context.getCurrentKey(), context.getValues(), context);
     *                 Iterator<VALUEIN> iter = context.getValues().iterator();
     *                 if (iter instanceof ValueIterator) {
     *                     ((ValueIterator)iter).resetBackupStore();
     *                 }
     *             }
     *         } finally {
     *             this.cleanup(context);
     *         }
     *
     *     }
     *
     * @param context
     * @throws IOException
     * @throws InterruptedException
     */
    @Override
    protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException {

//        if (values.iterator().hasNext()) {
//
//        }

        int sum = 0;
        for (IntWritable value : values) {
            sum += value.get();
        }

        outK.set(sum);
        context.write(key, outK);
    }

}

